<template>
  <div class="container">
    <div class="header">
      <el-input class="input" v-model="regionName" placeholder="请输入行政区域名称" clearable></el-input>
      <el-input class="input" v-model="regionCode" placeholder="请输入行政区域编码" clearable></el-input>
      <el-button class="btn" type="primary" icon="el-icon-search" @click="getData()">查找</el-button>
      <el-button class="btn" type="primary" icon="el-icon-download">导出</el-button>
    </div>

    <div class="table-box">
      <el-table :data="tableData" border style="width: 100%;">
        <el-table-column prop="id" label="区域ID" width="180" align="center">
        </el-table-column>
        <el-table-column prop="pid" label="区域父ID" width="180" align="center">
        </el-table-column>
        <el-table-column prop="name" label="区域名称" align="center">
        </el-table-column>
        <el-table-column prop="type" label="区域类型" align="center">
        </el-table-column>
        <el-table-column prop="code" label="区域编码" align="center">
        </el-table-column>
       
      </el-table>
    </div>

    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" background @current-change="handleCurrentChange" :current-page="currentPage"
        :page-sizes="[10,20,30,40]" :page-size="limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    

  </div>
</template>

<script>

import { getRegionData } from '../../api/mall-manage'


export default {
  name: 'region',
  data() {
    return {
      regionName: '',//行政区域名称
      regionCode: '',//行政区域编码
      tableData: [],//渲染的表格数据
      currentPage: 1,//当前页
      limit:20,//每页显示的数目
      total:0,//表格数据总数

    }
  },
  
  methods: {
    // 调接口获取数据
    getData() {
      getRegionData(this.currentPage,this.limit,this.regionName,this.regionCode).then(res => {
        let { data, errno } = res;
        if (errno == 0) {
          this.tableData.splice(0,this.tableData.length)
          this.total = data.total
          data.items.forEach(item => {
            this.tableData.push({
              id: item.id,
              pid: item.pid,
              name: item.name,
              type: item.type,
              code: item.code,
            })
          })
        }
      })
    },

    

    // 每页显示条目数改变事件
    handleSizeChange(e) {
      // console.log(e);
      this.limit = e
      this.getData()
    },

    // 当前页改变事件
    handleCurrentChange(e) {
      // console.log(e);
      this.currentPage = e
      this.getData()
      
    } 

  },
  mounted() {
    this.getData()
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.container {
  padding: 20px;

  .header {
    .input {
      width: 200px;
      margin-right: 5px;
    }
  }


  .table-box {
    width: 100%;
    margin-top: 30px;
  }

  .pagination{
    width: 100%;
    margin: 50px auto;
    padding: 0 30px;
  }
}
</style>